<?php
/**
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * 用户问卷
 * User: poptser
 * Date: 2017/8/10
 * Time: 14:28
 */
namespace SDXApi\Controller;
use SDXApi\Service\UserBehavior;
use Think\Controller;
use Think\Model;

class UsersQueController extends AppController
{
    /**
     * 检查是否问卷调查
     */
    public function is_questionnaire(){
        $user_id = I('post.uid');
        $is_que = M("users_ext")->where(array("user_id"=>$user_id,"sex"=>array("neq",0)))->count();
        if($is_que){
            returnApiSuccess(array("is_que"=>1),"已经调查过了");
        }else{
            $add['user_id'] = $user_id;
            $add['look_ext_time'] = gmtime();
            M("users_ext")->add($add,$options=array(),$replace=true);

            //添加行为
            $user_bevavior = new UserBehavior();
            $user_bevavior->add_browse($user_id, 91);
            returnApiSuccess(array("is_que"=>0),"还未调查过");
        }
    }

    /**
     * 提交数据
     */
    public function submit_questionnaire(){
        $run_time = S("submit_questionnaire");
        if(!empty($run_time)){
            returnApiError("请勿重复请求");
        }else{
            S("submit_questionnaire",999,10);
        }
        $user_id = I('post.uid');
        if(empty($user_id)){
            returnApiError("uid 为空");
        }
        $is_que = M("users_ext")->where(array("user_id"=>$user_id,"sex"=>array("neq",0)))->count();
        if($is_que){
            returnApiError("已经调查过了");
        }
        $user_sex = I("post.sex");  //男:1  女:2
        $user_age = I("post.age");  //年龄阶段  1:18-24 2:25-30  3:31-35 4:36-40 5:41-45 6:46-50 0:其他
        $user_family = I("post.family");    //家庭人口   1:单身贵族 2:神仙眷侣 3:一家三口 4:三代同堂
        $user_cycle = I("post.cycle");  //1:当天 2:1-2 3:3-4 7:一周
        $user_like = I("post.like");    //钟情品类 字符串 多个以,分割
        if(!in_array($user_sex,array(1,2))){
            returnApiError("性别选择错误:".$user_sex);
        }
        if(!in_array($user_age,array(0,1,2,3,4,5,6))){
            returnApiError("年龄段选择错误:".$user_age);
        }
        if(!in_array($user_family,array(1,2,3,4))){
            returnApiError("家庭状况选择错误:".$user_family);
        }
        if(!in_array($user_cycle,array(0,1,2,3,7))){
            returnApiError("买买买的频率选择错误:".$user_cycle);
        }
        if(empty($user_like)){
            returnApiError("钟情的品类:".$user_like);
        }

        //$add['user_id'] = $user_id;
        $add['sex'] = $user_sex;
        $add['age'] = $user_age;
        $add['family'] = $user_family;
        $add['cycle'] = $user_cycle;
        $add['like'] = $user_like;
        $add['update_ext_time'] = gmtime();
        $users_ext = M("users_ext")->where(array("user_id"=>$user_id))->save($add);
        if($users_ext){
            if(PLATFORM_TYPE=="formal"){
                $bonus_type_id = 299;
            }else{
                $bonus_type_id = 150;
            }

            $this->give_fun($bonus_type_id,$user_id);

            //添加行为
            $user_bevavior = new UserBehavior();
            $user_bevavior->add_browse($user_id, 92);
            returnApiSuccess(null,'成功');
        }else{
            returnApiError("失败");
        }
    }

    /**
     * 赠送红包
     * @param $bonus_type_id
     * @param $user_id
     * @return bool
     */
    public function give_fun($bonus_type_id,$user_id){
        if(empty($bonus_type_id)){
            return false;
        }
        $user_bonus = M("user_bonus");
        $bonus_type = M("bonus_type");

        $bonus_type_info= $bonus_type->field("has_effective_days,effective_days")->
        where(array("type_id"=>$bonus_type_id))->find();
        if(empty($bonus_type_info)){
            return false;
        }

        if($bonus_type_info['has_effective_days']==1 && $bonus_type_info['effective_days']>0){
            $end_time = time() + ($bonus_type_info['effective_days']-1)*24*3600;
            $end_date = date('Y-m-d',$end_time)." 23:59:59";
            $deadline = local_strtotime($end_date); //转换成GMT格林威治时间戳
        }else{
            $deadline = 0;
        }

        $bonus_id_f = $user_bonus->where(array("bonus_type_id"=>$bonus_type_id,"user_id"=>0))
            ->getField("bonus_id");
        if($bonus_id_f){
            $data['user_id'] = $user_id;
            $data['get_time'] = gmtime();
            $data['deadline'] = $deadline;
            $re = $user_bonus->where(array("bonus_id"=>$bonus_id_f))->save($data);
            if($re){
                return true;
            }else{
                return false;
            }
        }
    }
}